package com.sztz.handle;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.sztz.entity.User;
import com.sztz.vo.UserExcelVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息流式查询结果处理
 * @author yzy
 * @date 2022/7/26 16:50
 */
@Slf4j
@AllArgsConstructor
public class UserHandler extends AbstractResultHandler<User> {
    /**
     * EasyExcel写入处理器
     */
    private ExcelWriter excelWriter;
    /**
     * Excel写入的sheet页
     */
    private WriteSheet writeSheet;

    /**
     * 用户信息流式处理
     * @author yzy
     * @date 2022/08/01 16:43
     */
    @Override
    protected void handle() {
        log.info("UserHandler 需处理 {} 条数据",dataList.size());
        if(CollUtil.isNotEmpty(dataList)){
            WriteTable writeTable1 = EasyExcelFactory.writerTable(1).needHead(Boolean.TRUE).head(UserExcelVo.class).build();
            //实体对象转换成 ExcelVo对象
            List<UserExcelVo> collect = dataList.stream().map(User::convertToExcelVo).collect(Collectors.toList());
            excelWriter.write(collect , writeSheet,writeTable1);
            dataList.clear();
        }
    }
}
